var interval = null //倒计时函数
const app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
       canIUse: wx.canIUse('button.open-type.getUserInfo'),
       time: '获取验证码', //倒计时 
       currentTime: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       if(options.orderId){
             this.setData({
                  orderId: options.orderId
             })  
       }
  }, 
     mobileInput: function (e) {
          this.setData({
          mobile: e.detail.value
          })
     },
     codeInput: function (e) {
          this.setData({
          code: e.detail.value
          })

     },

     // 获取验证码
     getVerificationCode() {
          var that = this;
          let mobile = this.data.mobile
          var currentTime = that.data.currentTime

          if (mobile == "" || mobile == null) {
               wx.showToast({
                    title: '请输入手机号！',
                    icon: 'none',
                    duration: 1500
               })
               return
          }
          if (util.validatemobile(mobile)) {
               var data={"mobile":mobile}     
               var promise = app.synPostData('code/send', data)

               promise.then(function (res) {
                    if (res.data.code != 0) {
                         wx.showToast({
                              title: '系统错误',
                              icon: 'none',
                              duration: 1500
                         })
                    } else {
                         interval = setInterval(function () {
                              currentTime--;
                              that.setData({
                                   time: '（' + currentTime + '）'
                              })
                              if (currentTime <= 0) {
                                   clearInterval(interval)
                                   that.setData({
                                        time: '重新发送',
                                        currentTime: 60,
                                        disabled: false
                                   })
                              }
                         }, 1000)

                         that.setData({
                              disabled: true
                         })
                    }
               })
          }



     },
  bindGetUserInfo: function (e) {
          var encryptedData = e.detail.encryptedData
          var iv = e.detail.iv
          let that = this
          let mobile = that.data.mobile
          let code = that.data.code
          if (util.isBlank(mobile)) {
               wx.showToast({
                    title: '请输入手机号',
                    icon: 'none',
                    duration: 1500
               })
               return
          }
          if (util.isBlank(code)) {
               wx.showToast({
                    title: '请输入验证码',
                    icon: 'none',
                    duration: 1500
               })
               return
          }
          if (!encryptedData) {
               return
          }
          this.loinByWx(encryptedData, iv,mobile,code)
  },
     loinByWx: function (encryptedData, iv, mobile, code) {
          let that=this
          wx.login({
               success: res => {
                    var data = { 'code': res.code, 'memberId': app.globalData.userInfo.id, 'encryptedData': encryptedData, 'iv': iv, 'mobile': mobile,'validateCode':code}
                    var promise = app.synPostData('member/loginWithUnion', data)
                    promise.then(function (res) {
                         if (res.data.code != 0) {
                              wx.showToast({
                                   title: '登录失败',
                                   icon: 'none',
                                   duration: 1500
                              })
                         } else if(res.data.code==18){
                              wx.showToast({
                                   title: '验证码错误',
                                   icon: 'none',
                                   duration: 1500
                              })
                         } else {
                              app.globalData.userInfo = res.data.result
                              if (that.data.orderId) {
                                   wx.navigateTo({
                                        url: '../open-gift/open-gift?orderId=' + that.data.orderId
                                   })
                              } else {
                                   wx.switchTab({
                                        url: '../index/index'
                                   })
                              }

                         }
                    });
               }
          })
     }

})